package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.AddAccountRoleAuthorizationForm;
import com.woniuxy.dao.RoleInfoDao;
import com.woniuxy.dao.model.AccountRole;
import com.woniuxy.dao.AccountRoleDao;
import com.woniuxy.dao.model.RoleInfo;
import com.woniuxy.service.AccountRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Service
public class AccountRoleServiceImpl extends ServiceImpl<AccountRoleDao, AccountRole> implements AccountRoleService {

    @Resource
    private AccountRoleDao accountRoleDao;

    @Resource
    private RoleInfoDao roleInfoDao;

    @Override
    public void addAuthorization(AddAccountRoleAuthorizationForm aaraf) {
        //1.先删除账号对应的所有角色
        accountRoleDao.delete(new QueryWrapper<AccountRole>().eq("accountId",aaraf.getAccountId()));
        //2.再添加勾选的
        int[] ids = aaraf.getIds();

        for(int i = 0; i<ids.length;i++){
            AccountRole accountRole = new AccountRole();

            RoleInfo roleInfo = roleInfoDao.selectById(ids[i]);

            String roleLevel = roleInfo.getRoleLevel();

            accountRole.setAccountId(aaraf.getAccountId());

            accountRole.setRoleId(ids[i]);

            accountRole.setRoleLevel(roleLevel);

            accountRoleDao.insert(accountRole);

        }


    }
}
